#
# example001.py
#
# This example loads an audio wav file, next it plots their samples in time do-
# main and its DFT (Discrete Fourier Transform) using FFT (Fast Fourier Trans-
# form)
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

import getopt
import sys

from pylab import figure
from scipy.io.wavfile import read
from uoc.wave.plot import signal_spectrum
#from pylab import savefig

def example001(inputfile):
    print "example001.py"
    print
    #--------------------------------------------------------------
    # Reading data and obtaining basic attributes
    #--------------------------------------------------------------
    (sample_rate, samples) = read(inputfile)
    #--------------------------------------------------------------
    # Plot signal & frequency spectrum graphs
    #--------------------------------------------------------------
    fig = figure(num=None, figsize=(9, 9), dpi=80, facecolor='w', edgecolor='k')
    signal_spectrum(sample_rate, samples, fig)
    # Save plot
    # savefig("../plots/example001.py.1.png", format="png")
    # Show plot
    # fig.show()

def main(argv):
    inputfile = '../../../media/OSR_uk_000_0020_8k.wav'
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ifile="])
    except getopt.GetoptError:
        print 'example001.py -i <inputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'example001.py -i <inputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
    example001(inputfile)

if __name__ == "__main__":
    main(sys.argv[1:])
    print "Done"